package com.mdd.front.service.subject.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.course.Course;
import com.mdd.common.entity.subject.Subject;
import com.mdd.common.mapper.course.CourseMapper;
import com.mdd.common.mapper.subject.SubjectMapper;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.front.service.subject.ISubjectService;
import com.mdd.front.validate.PageParam;
import com.mdd.front.vo.course.CourseListVo;
import com.mdd.front.vo.subject.SubjectDetailVo;
import com.mdd.front.vo.subject.SubjectListVo;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 专题专区实现类
 * @author panweiliang
 */
@Service
public class SubjectServiceImpl implements ISubjectService {
        
    @Resource
    SubjectMapper subjectMapper;

    @Resource
    CourseMapper courseMapper;

    /**
     * 专题专区列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<SubjectListVo>
     */
    @Override
    public PageResult<SubjectListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        MPJQueryWrapper<Subject> mpjQueryWrapper = new MPJQueryWrapper<Subject>()
                .selectAll(Subject.class)
                .select("COUNT(sc.course_id) as courseCount, SUM(course.virtual_study_num + course.study_num) as studyNum")
                .innerJoin("?_subject_course sc ON sc.subject_id=t.id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .innerJoin("?_course course ON sc.course_id=course.id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .eq("t.status", "1")
                .groupBy("t.id")
                .orderByDesc(Arrays.asList("t.sort", "SUM(course.virtual_study_num + course.study_num)"));

        subjectMapper.setSearch(mpjQueryWrapper, params, new String[]{
                "like:name@t.name:str",
        });

        IPage<SubjectListVo> iPage = subjectMapper.selectJoinPage(new Page<>(page, limit), SubjectListVo.class, mpjQueryWrapper);

        for(SubjectListVo item : iPage.getRecords()) {
            item.setCover(UrlUtil.toAbsoluteUrl(item.getCover()));
            item.setImage(UrlUtil.toAbsoluteUrl(item.getImage()));
            item.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            item.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    /**
     * 专题专区详情
     *
     * @author panweiliang
     * @param id 主键参数
     * @return Subject
     */
    @Override
    public SubjectDetailVo detail(Integer id) {
        MPJQueryWrapper<Subject> mpjQueryWrapper = new MPJQueryWrapper<Subject>()
                .selectAll(Subject.class)
                .select("COUNT(sc.course_id) as courseCount, SUM(course.virtual_study_num + course.study_num) as studyNum")
                .innerJoin("?_subject_course sc ON sc.subject_id=t.id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .innerJoin("?_course course ON sc.course_id=course.id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .eq("t.status", "1")
                .eq("t.id", id)
                .groupBy("t.id");
        SubjectDetailVo vo = subjectMapper.selectJoinOne(SubjectDetailVo.class, mpjQueryWrapper);
        Assert.notNull(vo, "数据不存在");

        MPJQueryWrapper<Course> mpjCourseQueryWrapper = new MPJQueryWrapper<Course>()
                .selectAll(Course.class)
                .select("COUNT(catalogue.id) as catalogueCount")
                .innerJoin("?_subject_course sc ON sc.course_id = t.id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .leftJoin("?_course_catalogue catalogue ON catalogue.course_id = t.id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .eq("sc.subject_id", id)
                .eq("t.status", "1")
                .groupBy("sc.course_id");
//                .orderByDesc(Arrays.asList("sc.id"));
        List<CourseListVo> courses = courseMapper.selectJoinList(CourseListVo.class, mpjCourseQueryWrapper);
        for(CourseListVo item : courses) {
            item.setCover(UrlUtil.toAbsoluteUrl(item.getCover()));
        }
        vo.setCover(UrlUtil.toAbsoluteUrl(vo.getCover()));
        vo.setImage(UrlUtil.toAbsoluteUrl(vo.getImage()));
        vo.setCourses(courses);
        return vo;
    }

}
